package com.faasadmin.faas.modules.admin.admin.controller.system.appmarket;

import cn.hutool.setting.Setting;
import com.faasadmin.faas.modules.admin.admin.controller.system.appmarket.vo.SysAppMarketPageReqVO;
import com.faasadmin.faas.modules.admin.admin.controller.system.appmarket.vo.SysAppMarketPageRespVO;
import com.faasadmin.framework.common.pojo.CommonResult;
import com.faasadmin.framework.common.pojo.PageResult;
import com.faasadmin.framework.common.utils.BeanUtils;
import com.faasadmin.framework.common.utils.ObjectUtils;
import com.faasadmin.framework.common.utils.SettingUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import static com.faasadmin.framework.common.constant.Constants.*;

/**
 * @author: faasadmin
 * @description: 应用市场控制器
 * @data: 2022-10-09 17:04
 **/
@Slf4j
@Tag(name = "应用市场控制器")
@RestController
@RequestMapping("/system/app/market")
@Validated
@AllArgsConstructor
public class SysAppMarketController {

    @Resource
    private RestTemplate restTemplate;

    /**
     * 应用市场分页
     *
     * @param pageVO 请求参数
     * @return
     * @throws Exception
     */
    @GetMapping("/page")
    public CommonResult<PageResult<SysAppMarketPageRespVO>> getMgAppMarketPage(@Valid SysAppMarketPageReqVO pageVO) throws Exception {
        Setting setting = SettingUtils.readInstallSetting();
        String userName = setting.getByGroup(ACCOUNT_USER_NAME, ACCOUNT_CONFIG_GROUP);
        String password = setting.getByGroup(ACCOUNT_PASSWORD, ACCOUNT_CONFIG_GROUP);
        pageVO.setUserName(userName);
        pageVO.setPassword(password);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //提交参数设置
        Map<String, Object> stringObjectMap = BeanUtils.beanToMap(pageVO);
        MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
        for (Map.Entry<String, Object> entry : stringObjectMap.entrySet()) {
            if (ObjectUtils.isNotEmptyAndNull(entry.getValue())) {
                multiValueMap.put(entry.getKey(), Collections.singletonList(entry.getValue()));
            }
        }
        // 组装请求体
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(multiValueMap, headers);
        return restTemplate.postForObject(APP_MARKET_PAGE_URL, request, CommonResult.class);
    }
}
